package com.mchsdk.paysdk.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.mchsdk.paysdk.bean.PersonalCenterModel;
import com.mchsdk.paysdk.common.Constant;
import com.mchsdk.paysdk.http.process.ChangePasswordProcess;
import com.mchsdk.paysdk.utils.MCHInflaterUtils;
import com.mchsdk.paysdk.utils.MCLog;
import com.mchsdk.paysdk.utils.PreSharedManager;
import com.mchsdk.paysdk.utils.ToastUtil;
import com.mchsdk.paysdk.view.util.CheckImage;

public class MCChangePasswordActivity extends MCBaseActivity {
    private String TAG = "MCChangePasswordActivity";
    CheckImage checkImage;
    Activity context;

    String senAcc;
    String senPwd;

    EditText oldPwd;
    EditText newPwd;
    EditText subPwd;
    EditText cheNum;
    Button btnSub;

    String oldPwd_;
    String newPwd_;
    String subPwd_;
    String cheNum_;

    String checkNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(getLayout("activity_mch_personal_info_chgpwd"));
        initView();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        TextView txtTitle = (TextView) findViewById(MCHInflaterUtils.getControl(context, "tv_mch_header_title"));
        txtTitle.setText("修改密码");
        txtTitle.setGravity(Gravity.CENTER | Gravity.CENTER_VERTICAL);
        ImageView ivBack = (ImageView) findViewById(MCHInflaterUtils.getControl(this, "iv_mch_header_back"));
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(backClick);
        ImageView ivClose = (ImageView) findViewById(MCHInflaterUtils.getControl(this, "iv_mch_header_close"));
        ivClose.setVisibility(View.GONE);
        ivClose.setOnClickListener(backClick);
        senAcc = PersonalCenterModel.getInstance().channelAndGame.getAccount();
        senPwd = PersonalCenterModel.getInstance().channelAndGame.getPassword();

        oldPwd = (EditText) findViewById(getId("edt_oldPwd"));
        newPwd = (EditText) findViewById(getId("edt_newPwd"));
        subPwd = (EditText) findViewById(getId("edt_subPwd"));
        cheNum = (EditText) findViewById(getId("edt_cheNum"));
        btnSub = (Button) findViewById(getId("btnSub"));

        btnSub.setOnClickListener(subListener);
        checkImage = new CheckImage();

        checkNumber = checkImage.referchCheckImage(this, 100, 50);
        findViewById(getId("imageview")).setOnClickListener(referchCheckImageListener);
    }

    OnClickListener backClick = new OnClickListener() {

        @Override
        public void onClick(View v) {
            MCChangePasswordActivity.this.finish();
        }
    };
    OnClickListener referchCheckImageListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            checkNumber = checkImage.referchCheckImage(context, 100, 50);
        }
    };

    OnClickListener subListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            oldPwd_ = oldPwd.getText().toString();
            newPwd_ = newPwd.getText().toString();
            subPwd_ = subPwd.getText().toString();
            cheNum_ = cheNum.getText().toString();
            check();
        }
    };

    void check() {
        if (TextUtils.isEmpty(senAcc)) {
            ToastUtil.show(context, "未登录");
            return;
        }
        if (TextUtils.isEmpty(oldPwd_)) {
            ToastUtil.show(context, "原始密码不能为空");
            return;
        }
        if (!(oldPwd_.equals(senPwd))) {
            ToastUtil.show(context, "原始密码不正确");
            checkNumber = checkImage.referchCheckImage(context, 100, 50);
            return;
        }
        if (TextUtils.isEmpty(newPwd_)) {
            ToastUtil.show(context, "新密码不能为空");
            return;
        }
        if(!newPwd_.matches(Constant.REGULAR_ACCOUNT))
        {
            ToastUtil.show(context, "新密码格式不正确");
            return;
        }
        if (TextUtils.isEmpty(subPwd_)) {
            ToastUtil.show(context, "请输入确认密码");
            return;
        }
//        String regular = "^[a-zA-Z0-9]{6,15}$";
//        if (!newPwd_.matches(regular)) {// 改为正则表达式
//            ToastUtil.show(context, "密码格式不正确");
//            return;
//        }
        if (!(newPwd_.equals(subPwd_))) {
            ToastUtil.show(context, "新密码与确认密码不一致");
            return;
        }
        if (oldPwd_.equals(newPwd_)) {
            ToastUtil.show(context, "新密码与原始密码相同");
            return;
        }
        if (TextUtils.isEmpty(cheNum_)) {
            ToastUtil.show(context, "验证码不能为空");
            return;
        }
        if (!(cheNum_.equals(checkNumber))) {
            ToastUtil.show(context, "验证码输入有误");
            checkNumber = checkImage.referchCheckImage(this, 100, 50);
            return;
        }
        changePwd();
    }

    //出发更改密码请求方法
    private void changePwd() {
//		RequestParams params = createParams(senAcc, newPwd_);
//		ChangePasswordRequest changeRequest = new ChangePasswordRequest(myHandler);
//		changeRequest.post(myHandler);

        ChangePasswordProcess changePwdProcess = new ChangePasswordProcess();
        changePwdProcess.setPwd(oldPwd_);
        changePwdProcess.setRepwd(subPwd_);
        changePwdProcess.setCode("pwd");
        changePwdProcess.post(myHandler);

    }

    /**
     * 请求后根据结果执行操作
     */
    public Handler myHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.MODIFY_PASSWORD_SUCCESS:// 密码修改成功
                    MCLog.e(TAG, "update pwd onSuccess");
                    PersonalCenterModel.getInstance().channelAndGame.setPassword(newPwd_);
                    PreSharedManager.setString(Constant.PASSWORD, newPwd_, context);
                    //修改密码之后不要自动更新
//                    PersonalCenterModel.getInstance().channelAndGame.setPassword(null);
                    PreSharedManager.setString(Constant.PASSWORD, null, context);
                    ToastUtil.show(context, "密码修改成功");
                    MCChangePasswordActivity.this.finish();
                    break;
                case Constant.MODIFY_PASSWORD_FAIL:// 密码修改失败
                    String tip = (String) msg.obj;
                    if (TextUtils.isEmpty(tip)) {
                        tip = "密码修改失败";
                    }
                    ToastUtil.show(context, tip);
                    break;
                default:
                    break;
            }
        }
    };
}
